<?php

use Doraemon\service\UserFlow\Flow;
use Doraemon\tools\ActionLog;

class UserFlowAction extends ActionPlugin
{
    public function main()
    {

        \Doraemon\tools\Log::info('Userflow操作接收到的订阅消息', $this->messageBody);
        $uid = $this->messageBody['data']['uid'] ?? 0;

        $table = $this->messageBody['table'] ?? '';

        if ($table === 'brand_content') {
            $sBrandId = $this->messageBody['data']['brand_id'];
            $uid = \Biz\model\brand\Brand::getBrandUidById($sBrandId);
        }

        if (!$uid) {
            throw new \Exception('数据错误，用户uid不存在！');
        }

//        ActionLog::log($uid, json_encode($this->messageBody), ActionLog::USER_FLOW);

//        if (!$this->messageBody['database']) throw new \Exception('未获取到数据库');
//        if (!$this->messageBody['table']) throw new \Exception('未获取到数据表');
//        if (!$this->messageBody['data']) throw new \Exception('未接收到数据');

        //todo 测试数据
//        $json = '{"database":"hhzhome_content","table":"hhz_favorite_3","query":"","type":"insert","ts":1655116707,"xid":648946879,"commit":true,"position":"mysql-bin.000280:68844732","server_id":21738065,"thread_id":1074616026,"route_key":"2.6.12.14","trace_id":"","app_env":"16","data":{"id":8377139,"uid":19626693,"obj_id":"00010x50100boo8i","obj_type":2,"addtime":1655116707,"status":1,"ouid":19626930,"from_platform":1}}';

//        $json = '{"database":"hhzhome_content","table":"hhz_photos_3","query":"","type":"update","ts":1655176623,"xid":706674179,"commit":true,"position":"mysql-bin.000280:203058222","server_id":21738065,"thread_id":1074691891,"route_key":"7.85.94.144","trace_id":"","app_env":"16","data":{"id":"0009vdi0000boo1x","uid":19626693,"house_type":0,"type":0,"article_id":"","title":"","remark":"14-1","admin_tag":"","group_tag":"","quality":3,"admin_score":30,"pic_id":"3f11f21kw1kw0TE00rdg5omboo1x","pic_org_id":"3f11f21kw1kw0TE00rdg5omboo1x","video_id":"","pic_list":"[{\"pic_id\":\"3f11f21kw1kw0TE00rdg5omboo1x\",\"pic_org_id\":\"3f11f21kw1kw0TE00rdg5omboo1x\",\"has_tags\":0,\"has_wiki\":0,\"has_old_tags\":0,\"has_goods\":0}]","is_album":0,"has_goods":0,"mention_uids":"[]","show_type":0,"resource_type":0,"is_best":0,"is_bottom":0,"topic_activity_id":0,"addtime":1655175622,"edittime":0,"user_update_time":0,"add_ip":"172.30.5.1","status":1,"show_status":1,"is_origin":1,"image_type":"2","is_fold":0,"pic_num":1,"address_num":0,"address_name":"","activity_id":0,"img_tags":"","new_img_tags":"","subject":""},"old":{"quality":0,"admin_score":0,"status":0,"show_status":4}}';


        //new Flow(json_decode($json, true));
        $flow = new Flow();
        $flow->run($this->messageBody);
        $this->setSuccess();
    }
}
